package forum.klanmichalakow.org;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Forum {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		fullSize(driver);

		login(driver);
		wait10sec(driver);
		logout(driver);
		close(driver);

	}

	static void fullSize(WebDriver driver) {
		driver.manage().window().setSize(new Dimension(1366, 768));
	}

	protected static void login(WebDriver driver) {
		driver.get("http://klanmichalakow.org/forum");
		System.out.println("Page title is: " + driver.getTitle());
		driver.findElement(
				By.xpath(".//*[@id='menubar']/table/tbody/tr/td[1]/a[1]"))
				.click();
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[2]/input"))
				.sendKeys("karol");
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td[2]/input"))
				.sendKeys("OakY44T0ot");
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[3]/td/input[3]"))
				.click();
		driver.findElement(By.xpath("//*[text()[contains(.,'karol')]]"))
				.isDisplayed();
		wait10sec(driver);

	}

	protected static void logout(WebDriver driver) {
		driver.findElement(
				By.xpath(".//*[@id='menubar']/table/tbody/tr/td[1]/a[1]"))
				.click();

	}

	protected static void wait10sec(WebDriver driver) {

		WebDriverWait wait = new WebDriverWait(driver, 10);
		wait.until(ExpectedConditions.presenceOfElementLocated(By
				.xpath(".//*[@id='logodesc']/table/tbody/tr/td[1]/a/img")));

	}

	protected static void close(WebDriver driver) {
		driver.quit();
	}

	protected static void addNewThread(WebDriver driver) {

		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/table[2]/tbody/tr[12]/td[2]/a"))
				.click();
		driver.findElement(
				By.xpath(".//*[@id='pagecontent']/table[1]/tbody/tr/td[1]/a/img"))
				.click();
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[2]/td[2]/input"))
				.sendKeys("Tytul - testowy");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[3]/td[2]/table/tbody/tr[4]/td[1]/textarea"))
				.sendKeys("Przykladowy tekst");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[6]/td/input[2]"))
				.click();

		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

	}

	protected static void updateNewThread(WebDriver driver) {
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/table[2]/tbody/tr[12]/td[2]/a"))
				.click();
		driver.findElement(
				By.xpath(".//*[@id='pagecontent']/table[2]/tbody/tr[3]/td[2]/a"))
				.click();

		driver.findElement(
				By.xpath(".//*[@id='pagecontent']/table[3]/tbody/tr[4]/td[2]/div[2]/a[1]/img"))
				.click();
		// driver.findElement(By.xpath("")).click();

		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[4]/td[2]/table/tbody/tr[4]/td[1]/textarea"))
				.sendKeys(" Przykladowy tekst - update");
		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/form/table/tbody/tr[8]/td/input[2]"))
				.click();

	}

	protected static void deleteNewThread(WebDriver driver) {

		driver.findElement(
				By.xpath(".//*[@id='wrapcentre']/table[2]/tbody/tr[12]/td[2]/a"))
				.click();
		driver.findElement(
				By.xpath(".//*[@id='pagecontent']/table[2]/tbody/tr[3]/td[2]/a"))
				.click();

		driver.findElement(
				By.xpath(".//*[@id='pagecontent']/table[3]/tbody/tr[3]/td[2]/table/tbody/tr/td/table/tbody/tr/td/a[3]/img"))
				.click();

		driver.findElement(
				By.xpath(".//*[@id='pagecontent']/form/table/tbody/tr[2]/td/input[7]"))
				.click();

	}

}
